package com.tpc.server.systeme.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateManager {

    private Calendar objCAL = null;

    public DateManager() {
        objCAL = Calendar.getInstance();
    }
    public long getMillisecondes() {
        return objCAL.getTimeInMillis();
    }
    public java.sql.Timestamp getTimeStamp() {
        return new java.sql.Timestamp(objCAL.getTimeInMillis());
    }
    public String NOW() {
        return YEAR()+"-"+MONTH()+"-"+DAY_OF_MONTH()+" "+HOUR()+":"+MINUTE()+":00";
    }

    public String NOW_NOTIME() {
        return YEAR()+"-"+MONTH()+"-"+DAY_OF_MONTH();
    }

    public String EMPTY() {
        return "0000-00-00 00:00:00";
    }
    
    public String calculateNextDate(String strDate,int intDaysToAdd) {
        try {
            //create Calendar from string
            Calendar objCalTemp = Calendar.getInstance();
            int intMonthFromZero = new Integer(strDate.substring(5,7)).intValue();
            objCalTemp.set(new Integer(strDate.substring(0,4)).intValue(),intMonthFromZero-1,new Integer(strDate.substring(8)).intValue());
            objCalTemp.add(Calendar.DAY_OF_MONTH,intDaysToAdd);
            return objCalTemp.get(Calendar.YEAR)+"-"+MONTH(objCalTemp.get(Calendar.MONTH),true)+"-"+DAY_OF_MONTH(objCalTemp.get(Calendar.DAY_OF_MONTH));
        }
        catch (Exception objEX) {

            return null;
        }
    }
    public Calendar getMonday(java.util.Calendar objCalendarP) {
        java.util.Calendar objCaltemp = objCalendarP;
        if (objCaltemp.get(java.util.Calendar.DAY_OF_WEEK) > 1) {
            int intOffsetFirstDay = objCaltemp.get(java.util.Calendar.DAY_OF_WEEK)-2;
            objCaltemp.add(java.util.Calendar.DAY_OF_WEEK,-intOffsetFirstDay);
        }
        else { objCaltemp.add(java.util.Calendar.DAY_OF_WEEK,1); }
        return objCaltemp;
    }
    public Calendar getFriday(java.util.Calendar objCalendarP) {
        java.util.Calendar objCaltemp = getMonday(objCalendarP);
        objCaltemp.add(java.util.Calendar.DAY_OF_WEEK,4);
        return objCaltemp;
    }
    public String YEAR() {
        return new Integer(objCAL.get(Calendar.YEAR)).toString();
    }
	
    public String MONTH() {
        String strPrefix = "";
        int intMonth = objCAL.get(Calendar.MONTH);
        if (intMonth < 9) { strPrefix = "0"; }
        String strMONTH = new Integer(intMonth+1).toString();
        strMONTH = strPrefix+strMONTH;
        return strMONTH;
    }
    public String MONTH(int intMonth) {
        String strPrefix = "";
        if (intMonth < 9) { strPrefix = "0"; }
        String strMONTH = new Integer(intMonth).toString();
        strMONTH = strPrefix+strMONTH;
        return strMONTH;
    }
    public String MONTH(int intMonth,boolean blnAddOne) {
        String strPrefix = "";
        if (intMonth < 9) { strPrefix = "0"; }
        String strMONTH = new Integer(intMonth+1).toString();
        strMONTH = strPrefix+strMONTH;
        return strMONTH;
    }
	
    public String MONTH_TXT() {
        String[] arrMONTH = {"janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"};
        int intMonth = objCAL.get(Calendar.MONTH);
        return arrMONTH[intMonth];
    }

    public String DAY_OF_MONTH() {
            String strDAY_OF_MONTH = new Integer(objCAL.get(Calendar.DAY_OF_MONTH)).toString();
            if (objCAL.get(Calendar.DAY_OF_MONTH) < 10) {
                    strDAY_OF_MONTH = "0"+strDAY_OF_MONTH;
            }
            return strDAY_OF_MONTH;
    }
    public String DAY_OF_MONTH(int intDayOfMonth) {
            String strDAY_OF_MONTH = new Integer(intDayOfMonth).toString();
            if (intDayOfMonth < 10) {
                    strDAY_OF_MONTH = "0"+strDAY_OF_MONTH;
            }
            return strDAY_OF_MONTH;
    }
    public String HOUR() {
            String strHOUR = new Integer(objCAL.get(Calendar.HOUR_OF_DAY)).toString();
            if (objCAL.get(Calendar.HOUR_OF_DAY) < 10) {
                    strHOUR = "0"+strHOUR;
            }
            return strHOUR;
    }
	
    public int HOUR_INT24() {
            return objCAL.get(Calendar.HOUR_OF_DAY);
    }

    public String MINUTE() {
            String strMINUTE = new Integer(objCAL.get(Calendar.MINUTE)).toString();
            if (objCAL.get(Calendar.MINUTE) < 10) {
                    strMINUTE = "0"+strMINUTE;
            }
            return strMINUTE;
    }

    public String transformTStoString(java.sql.Timestamp datToTransform) {
            String strDate = "0000-00-00";
            if (datToTransform != null) {
                    strDate = datToTransform.toString();
            }
            if (strDate.length() > 9) {
                    strDate = strDate.substring(0,10);
            }
            return strDate;
    }
    
    public String DATE_NO_TIME(java.util.Calendar objCalT) {
        String strDateNoTime = "";
        strDateNoTime = strDateNoTime+objCalT.get(Calendar.YEAR);
        strDateNoTime = strDateNoTime+"-"+MONTH(objCalT.get(Calendar.MONTH));
        strDateNoTime = strDateNoTime+"-"+DAY_OF_MONTH(objCalT.get(Calendar.DAY_OF_MONTH));
        return strDateNoTime;
    }
    public String DATE_NO_TIMEBD(java.util.Calendar objCalT) {
        String strDateNoTime = "";
        strDateNoTime = strDateNoTime+objCalT.get(Calendar.YEAR);
        strDateNoTime = strDateNoTime+"-"+MONTH(objCalT.get(Calendar.MONTH)+1);
        strDateNoTime = strDateNoTime+"-"+DAY_OF_MONTH(objCalT.get(Calendar.DAY_OF_MONTH));
        return strDateNoTime;
    }
    public Calendar getCAL() {
            return objCAL;
    }
    
    public String getStartMonth() {
        return YEAR()+"-"+MONTH()+"-01 00:00:00";
    }
    
    public String getEndOfCurrentMonth(String dataBaseDateFormat) {
        SimpleDateFormat sdfDDmmYYYY = new SimpleDateFormat(dataBaseDateFormat,Locale.US);
        Calendar calNow = Calendar.getInstance();
        Calendar endCal = calNow;
/*** DO NOT DELETE : objlog.info we need a GET before set is SET ! ****/
System.out.println("saturday:"+endCal.get(Calendar.DAY_OF_MONTH)+" mois :"+endCal.get(Calendar.MONTH));
        endCal.set(Calendar.HOUR,23);
        endCal.set(Calendar.MINUTE,59);
        endCal.set(Calendar.SECOND,59);
        endCal.set(Calendar.DAY_OF_MONTH,endCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endDate = sdfDDmmYYYY.format(endCal.getTime());
        return endDate;
    }
}
